
/**
 * Created by Administrator on 2017/11/16 0016.
 */
var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var CONFIG = require('../../config');


var YjxPptUserDocModel = function (obj) {
    YjxBaseModel.call(this,obj);
};

YjxPptUserDocModel.prototype = new YjxBaseModel();

// 取得表名
YjxPptUserDocModel.prototype.getTableName = function(){
    return 'yjx_ppt_user_doc';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxPptUserDocModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'doc_id='+this.doc_id;
};


/**
 * 修改作品名
 *
 * @author 严旭东
 * @date 2017-11-17
 *
 * @param userId 用户唯一标识
 * @param docId 作品唯一标识
 * @param name 用户修改名
 * @param callback  回调
 *          err: 查询失败
 *          results : {}
 */
YjxPptUserDocModel.prototype.updataPptUserDocName = function(userId,docId,name,callback){
    pool.pool.getConnection(function(getConnectErr,connection) {
        if (getConnectErr) {
            console.log('YjxPptUserDocModel.updataVideoUserDoc getConnectErr: ' + getConnectErr);
            return callback(getConnectErr, null);
        }
        var sql =  ' UPDATE yjx.yjx_ppt_user_doc ' +
                    '  SET doc_name = "'+name +'" '  +
                    '  WHERE user_id = ' + userId +
                    '  AND uni_doc_id =  ' + docId;
        console.log(sql);
        connection.query(sql, function (error, results) {
            connection.release();
            if (error) {
                console.log('YjxPptUserDocModel.updataVideoUserDoc Err: ' + error);
                return callback(error, null);
            }
            return callback(null, results);
        })
    })
};

/**
 * 查询出“用户文档页”对应的“模版文档数据”
 *
 * @author 王俊
 * @date 2017-11-20
 *
 * @param user_doc_id   用户doc id
 * @param callback  回调，参数
 *          error:错误,null表示无错误
 *          documentModel:模版文档数据,ItbDocumentModel的数组
 * 注意: 复制自 ItbDocumentModel.prototype.getTemplateDocumentByUserDocId 具体功能待测
 */
YjxPptUserDocModel.prototype.getTemplateDocumentByUserDocId = function(user_doc_id,callback){
    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbPageModel.getTemplateDocumentByUserDocId Err:'+err);
        }
        // Use the connection
        var sql =
            ' select * from itbour.itb_template d1 '+
            ' where d1.tmpl_id=( '+
            ' select d.base_doc_id from yjx.yjx_ppt_user_doc d '+
            ' where d.doc_id='+user_doc_id+
            ' and d.user_type=1 '+
            ' and d.del_flg=0) ';

        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            callback(error, result);
        });
    });
};


module.exports = YjxPptUserDocModel;